﻿#include "qprintermanager.h"
#include <QFile>
#include <QTextStream>
#include <QWebView>
#include <QFileInfo>
#include <QFileDialog>
#include <QPrintDialog>
#include <QPageSize>

QPrinterManager::QPrinterManager(QObject *parent) : QObject(parent)
{

}

void QPrinterManager::setPrintSetting(PrintSetting *printSetting){
    this->printSetting = printSetting;
}

void QPrinterManager::setHeaderList(QStringList headerList)
{
    this->headerList = headerList;
}

void QPrinterManager::insertRowData(QStringList rowData)
{
    tableData.append("<tr>");
    for(QString colData:rowData){
        tableData.append(QString("<td width=100px height=10px>%1</td>").arg(colData));
    }
    tableData.append("</tr>");
}

void QPrinterManager::preparePrint(QStandardItemModel *model)
{
    QStringList headers;
    for(int col=0; col<model->columnCount(); col++){
        headers.append(model->horizontalHeaderItem(col)->text());
    }

    this->tableData.clear();
    setHeaderList(headers);
    for(int row=0; row<model->rowCount()-1; row++){
        QStringList rowData;
        for(int col=0; col<model->columnCount(); col++){
            rowData.append(model->item(row, col)->text());
        }
        insertRowData(rowData);
    }
}

void QPrinterManager::preview()
{
    QPrinter printer;
    QPrintPreviewDialog *previewDialog = new QPrintPreviewDialog(&printer);

    connect(previewDialog, &QPrintPreviewDialog::paintRequested, this, &QPrinterManager::getPreviewData);
    previewDialog->resize(1024,800);
    previewDialog->exec();
}

void QPrinterManager::print()
{
//    QPrinter printer(QPrinter::HighResolution);
//    QPrintDialog printDialog(&printer);
//    if(printDialog.exec() == QDialog::Accepted){
//        QWebView webView;
//        webView.resize(800, 600);
//        webView.setHtml(getTemplateHtml());
//        printer.setPageSize(QPrinter::Custom);
//        printer.setPaperSize(QSizeF(800, 600), QPrinter::Point);
//        webView.print(&printer);
//    }
    QString fileName = QFileDialog::getSaveFileName(nullptr, tr("导出PDF文件"), QString(), "*.pdf");
        if (!fileName.isEmpty()) {
            // 如果文件后缀为空，则默认使用.pdf
            if (QFileInfo(fileName).suffix().isEmpty())
                fileName.append(".pdf");
            QPrinter printer;
            // 指定输出格式为pdf
            printer.setOutputFormat(QPrinter::PdfFormat);
            printer.setOutputFileName(fileName);
            QWebView webView;
            webView.setHtml(getTemplateHtml());
            webView.print(&printer);
        }
}

QString QPrinterManager::getHearderHtml()
{
    QString headerHtml;

    for(QString header:headerList)
    {
        headerHtml.append(QString("<td width=100px height=10px>%1</td>").arg(header));
    }

    return headerHtml;
}

QString QPrinterManager::getTemplateHtml()
{
    QFile x(":/template/template/print_template.html");
    x.open(QIODevice::ReadOnly);
    QTextStream in(&x);
    in.setCodec("UTF-8"); //注意编码
    QString html = in.readAll();

    // "none"
    QString headerStatus = (printSetting->headerItem6.isEmpty() &&
                            printSetting->headerItem7.isEmpty() &&
                            printSetting->headerItem8.isEmpty() &&
                            printSetting->headerItem9.isEmpty() &&
                            printSetting->headerItem10.isEmpty())?"none":"table-cell";
    QString tailStatus = (printSetting->tailItem6.isEmpty() &&
                          printSetting->tailItem7.isEmpty() &&
                          printSetting->tailItem8.isEmpty() &&
                          printSetting->tailItem9.isEmpty() &&
                          printSetting->tailItem10.isEmpty())?"none":"table-cell";
    html = html.arg(printSetting->title)
            .arg("table-cell").arg(printSetting->headerItem1)
            .arg("table-cell").arg(printSetting->headerItem2)
            .arg("table-cell").arg(printSetting->headerItem3)
            .arg("table-cell").arg(printSetting->headerItem4)
            .arg("table-cell").arg(printSetting->headerItem5)
            .arg(headerStatus).arg(printSetting->headerItem6)
            .arg(headerStatus).arg(printSetting->headerItem7)
            .arg(headerStatus).arg(printSetting->headerItem8)
            .arg(headerStatus).arg(printSetting->headerItem9)
            .arg(headerStatus).arg(printSetting->headerItem10)
            .arg(getHearderHtml())
            .arg(tableData)
            .arg("table-cell").arg(printSetting->tailItem1)
            .arg("table-cell").arg(printSetting->tailItem2)
            .arg("table-cell").arg(printSetting->tailItem3)
            .arg("table-cell").arg(printSetting->tailItem4)
            .arg("table-cell").arg(printSetting->tailItem5)
            .arg(tailStatus).arg(printSetting->tailItem6)
            .arg(tailStatus).arg(printSetting->tailItem7)
            .arg(tailStatus).arg(printSetting->tailItem8)
            .arg(tailStatus).arg(printSetting->tailItem9)
            .arg(tailStatus).arg(printSetting->tailItem10);

    return html;
}

void QPrinterManager::getPreviewData(QPrinter *printer)
{
    QWebView *webView = new QWebView();
    webView->resize(600,500);
    webView->setHtml(getTemplateHtml());
    webView->print(printer);
}
